<?php

namespace app\api\controller;

use app\api\model\Student as StudentModel;
use think\facade\Filesystem;
use think\facade\Request;

class Student
{

    public $studentData;

    public function __construct()
    {
        $token = input('get.token');
        if (empty($token)) output_error('token为空');
        $this->studentData = StudentModel::where('member_token', '=', $token)->find();
        if (empty($this->studentData)) output_error('token无效');
    }

    public function getData()
    {
        unset($this->studentData['member_password']);
        unset($this->studentData['member_token']);
        output_data($this->studentData);
    }

    public function modifyName()
    {
        $name = input('get.name');
        if (empty($name)) output_error('昵称不能为空');
        StudentModel::where('member_id', '=', $this->studentData['member_id'])
            ->update(array('member_name' => $name));
        output_data(1);
    }

    public function modifyAvatar()
    {
        $file = request()->file('image');
        $savename = Filesystem::disk('avatar')
            ->putFile('', $file, 'uniqid');
        if ($this->studentData['member_avatar'] != 'default.png') {
            unlink(app()->getRootPath() . 'public\\upload\\avatar\\'
                . $this->studentData['member_avatar']);
        }
        StudentModel::where('member_id', '=', $this->studentData['member_id'])
            ->update(array('member_avatar' => $savename));
        output_data($savename);
    }

}

